package JavaThread;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


@Slf4j(topic = "c. CreateThread")
public class CreateThread {

    @Test
    public void test1() throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                log.debug("running");
            }
        };
        t.setName("t1");
        t.start();
        //Thread.sleep(1000);
        log.debug("running");
    }//两个线程并行运行

    @Test
    public void Test2() {
        Runnable r = new Runnable(){
            @Override
            public void run() {
                log.debug("running");
            }
        };
        Thread t = new Thread(r, "t2");
        t.start();
        log.debug("running");
    }

    @Test
    public void test3(){
        //两种简化方式
        Runnable r = () -> log.debug("running");
        Thread t = new Thread(r, "t3");
        t.start();

        Thread t1 = new Thread(() -> log.debug("running"));
        t1.start();
    }

    @Test
    public void test4() throws ExecutionException, InterruptedException {
        //两种简化方式
        FutureTask futureTask = new FutureTask<>(() -> {
            log.debug("running");
            Thread.sleep(1000);
            return 100;
        });
        Thread t = new Thread(futureTask, "t4");
        t.start();
        log.debug("{}",futureTask.get());

    }

}
